import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';

type Resources = {
  common: {
    welcome: string;
    settings: string;
    logout: string;
  };
  home: {
    title: string;
    description: string;
    features: {
      title: string;
      item1: string;
      item2: string;
    };
  };
  menu: {
    homeVisualData: string;
    shoppingList: string;
    orderForm: string;
    aiDataAnalysis: string;
    addShopping: string;
  };
};

declare module 'i18next' {
  interface CustomTypeOptions {
    resources: Resources;
  }
}

import commonZh from './zh/common.json';
import commonEn from './en/common.json';
import commonJa from './ja/common.json';
import commonKo from './ko/common.json';

import homeZh from './zh/home.json';
import homeEn from './en/home.json';
import homeJa from './ja/home.json';
import homeKo from './ko/home.json';

import menuZh from './zh/menu.json';
import menuEn from './en/menu.json';
import menuJa from './ja/menu.json';
import menuKo from './ko/menu.json';

import shopZh from './zh/shoppingList.json';
import shopEn from './en/shoppingList.json';
import shopJa from './ja/shoppingList.json';
import shopKo from './ko/shoppingList.json';

i18n
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    resources: {
      zh: { common: commonZh, home: homeZh, menu: menuZh, shop:  shopZh },
      en: { common: commonEn, home: homeEn, menu: menuEn, shop:  shopEn },
      ja: { common: commonJa, home: homeJa, menu: menuJa, shop:  shopJa },
      ko: { common: commonKo, home: homeKo, menu: menuKo, shop:  shopKo }
    },
    fallbackLng: 'zh',
    ns: ['common', 'home'],
    defaultNS: 'common',
    interpolation: {
      escapeValue: false,
    },
    detection: {
      order: ['localStorage', 'navigator'],
      caches: ['localStorage']
    }
  });

export default i18n;